{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python\n",
    "# coding: utf-8\n",
    "#引入numpy 库\n",
    "#import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from numba import jit,njit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rand_bv(n):\n",
    "    return np.random.rand(n)>0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "bv1=rand_bv(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(bv1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@jit\n",
    "#定义布尔值之间运算\n",
    "def otimes_b_b (a,b):\n",
    "    if a==b:\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "#布尔向量定义为python中的列表 list\n",
    "#定义布尔向量与布尔值之间计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "@jit\n",
    "def otimes_bv_b(bv,b):\n",
    "    return [otimes_b_b(x,b) for x in bv]\n",
    "#定义布尔向量之间运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "@jit\n",
    "def otimes_bv_bv(bv1,bv2):\n",
    "    temp=[otimes_b_b(x,y) for x,y in zip(bv1,bv2)]\n",
    "    #如果两个向量之间相同位置布尔值相同的部分大于不同的部分\n",
    "    if temp.count(True)>temp.count(False):\n",
    "        #返回 True\n",
    "        return True\n",
    "    else:\n",
    "        #否则返回 False\n",
    "        return False\n",
    "#矩阵定义为numpy中的array\n",
    "#定义布尔矩阵之间运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 0])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1,1,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True,  True,  True,  True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(np.arange(1,10),dtype=bool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True],\n",
       "       [ True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "         True]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(np.random.rand(10, 10),dtype=bool)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False,  True,  True, False, False, False, False,  True,\n",
       "       False])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand(10)>0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "        True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "        True,  True,  True,  True,  True,  True,  True,  True,  True,\n",
       "        True,  True,  True,  True,  True])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(32).astype(np.bool)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "from numba.types import bool_, int_, float32\n",
    "\n",
    "@njit(bool_[:,:](float32[:,:,:],float32[:,:,:],int_))\n",
    "def test(im1, im2, j_delta=1):\n",
    "    diff = ((im1 - im2)**2).sum(axis=2)/3\n",
    "    mask = np.zeros_like(diff, bool_)  # <--- like so\n",
    "    for i in range(diff.shape[0]):\n",
    "        for j in range(diff.shape[1]):\n",
    "            mask[i,j] = diff[i,j] > 1.0\n",
    "    return mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(10)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def otimes_bm_bm(bm1,bm2):\n",
    "    #后一个矩阵列向量变为行向量\n",
    "    bm2_t=bm2.T\n",
    "    temp =[otimes_bv_bv(x,y) for x in bm1\n",
    "              for y in bm2_t]\n",
    "    temp =np.array(temp)  \n",
    "    return temp.reshape((bm1.shape[0],bm2.shape[1]))\n",
    "#定义矩阵与向量之间运算\n",
    "def otimes_bm_bv(bm,bv):\n",
    "    temp=[otimes_bv_bv(x,bv) for x in bm]\n",
    "    return temp\n",
    "#定义矩阵与向量之间运算\n",
    "def otimes_bv_bm(bv,bm):\n",
    "    temp=[otimes_bv_bv(x,bv) for x in bm]\n",
    "    return temp    \n",
    "#定义矩阵与布尔值之间计算\n",
    "def otimes_bm_b(bm,b):\n",
    "    temp=[otimes_bv_b(x,b) for x in bm]\n",
    "    return np.array(temp)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "def  qtimes_bv_bv(bv1,bv2):\n",
    "    temp =[otimes_b_b(x,y) for x,y in zip(bv1,bv2)]\n",
    " \n",
    "    return temp   \n",
    "\n",
    "def qtimes_bv_bm(bv1,bm1):\n",
    "    temp=[qtimes_bv_bv(bv1,x) for x in (list(bm1))]\n",
    "    return np.array(temp)\n",
    "\n",
    "def rand_bv(n):\n",
    "    a=np.random.rand(1,n)\n",
    "    x=a>0.5\n",
    "    return x[0].tolist()  \n",
    "\n",
    "\n",
    "def qtimes_loss(bv1,bv2):\n",
    "    temp = qtimes_bv_bv(bv1,bv2)\n",
    "    return temp.count(False)/len(temp)\n",
    "\n",
    "\n",
    "def perfect_match_bv_bv(bv1,bv2):\n",
    "\tif bv1==bv2:\n",
    "\t\treturn True\n",
    "\telse:\n",
    "\t\treturn False\n",
    "\n",
    "def perfect_match_bv_bm(bv1,bm1):\n",
    "    temp=[perfect_match_bv_bv(bv1,list(x)) for x in (list(bm1))]\n",
    "    return temp\n",
    "\n",
    "\n",
    "def convolution(gfun, oa, ob):\n",
    "    #n=0\n",
    "    res=[]\n",
    "    oa_rows=oa.shape[0]\n",
    "    oa_cols=oa.shape[1]\n",
    "    ob_rows=ob.shape[0]\n",
    "    ob_cols=ob.shape[1]\n",
    "    for i in range (0,(oa_rows-ob_rows+1)):\n",
    "        #print (i)\n",
    "        for j in range (0,(oa_cols-ob_cols+1)):\n",
    "            temp=((oa[i:(i+ob_rows),:])[:,j:(j+ob_cols)])\n",
    "            tempx=temp.reshape(1,temp.shape[0]*temp.shape[1]).tolist()[0]\n",
    "            tempy=ob.reshape(1,ob.shape[0]*ob.shape[1]).tolist()[0]\n",
    "            res_temp=gfun(tempx,tempy)\n",
    "            res.append(res_temp)\n",
    "            #print(b)\n",
    "            #show_nparray(b)\n",
    "            #n=n+1\n",
    "    res=np.array(res)\n",
    "    #print(n)\n",
    "\n",
    "    return res.reshape((oa_rows-ob_rows+1),(oa_cols-ob_cols+1))  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
